window.addEvent('domready', function() {
	var paramRequest = function() {
		var data = {
				response: null
		};

		var key = new Request({
			url: 'index.php',
			async: false,
			onSuccess: function(r) {
				data.response = r;
			}
		});
			
		return {
			getKey: function() {
				key.send('option=com_syncr&task=keyGenerator&format=raw');
				return data.response;
			}
		}
	}();
	
	// generate api button
	$$('button.generatekey').addEvent('click', function(e) {
		// stop propagation
		e.stop();
		
		// field
		apifield = this.getPrevious();
		
		if (apifield.value === "") {
			// generate key
			apifield.value = paramRequest.getKey();
		} else {
			// key already set
			if (confirm("There is already a key assigned to this site. If you generate a new one, already paired sites will not sync.")) { 
				// generate key
				apifield.value = paramRequest.getKey();
			}
		}
	});
});